package cn.xunjie.org.service.impl;

import cn.xunjie.basic.exception.BusinessException;
import cn.xunjie.basic.service.impl.BaseServiceImpl;
import cn.xunjie.basic.util.BaiduAuditUtils;
import cn.xunjie.org.domain.Employee;
import cn.xunjie.org.domain.Shop;
import cn.xunjie.org.domain.ShopAuditLog;
import cn.xunjie.org.mapper.EmployeeMapper;
import cn.xunjie.org.mapper.ShopAuditLogMapper;
import cn.xunjie.org.mapper.ShopMapper;
import cn.xunjie.org.service.IShopService;
import cn.xunjie.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiaosang
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;



    @Override
    public void settlement(Shop shop) {
        System.out.println(shop);
        Employee admin = shop.getAdmin();
        //1参数校验 判断 name，address，username是否为空
        if(StringUtils.isEmpty(shop.getName())
            || StringUtils.isEmpty(shop.getAddress())
            || StringUtils.isEmpty(admin.getUsername())
        ){
            throw new BusinessException("不能为空");
        }
        //2合法校验明天做
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("不要骚操作");
        }
        //3判断是否曾经注册过
        Shop shopTmp = shopMapper.loadByName(shop.getName());

        if(shopTmp != null){
            throw new BusinessException("店铺已经被注册，请直接登录");
        }
        //3.1店铺不能被注册
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());

        if(empTmp != null){
            throw new BusinessException("已存在请直接登录");
        }
        //3.2注册了并已经绑定了店铺
        //注册了但是没有绑定店铺 === 直接绑定  要返回自增id
        if(empTmp == null){
            employeeMapper.save(admin);
            empTmp = admin;
        }
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);

        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);

    }

    @Override
    public void pass(ShopAuditLog log) {//shop id 和 文章
        //1.修改店铺状态为---审核通过
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
        log.setAuditId(333L);
        shopAuditLogMapper.update(log);
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("123720197@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻激活请求");
        message.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        javaMailSender.send(message);

    }

    @Override
    public void reject(ShopAuditLog log) {
        //1.修改店铺状态为---审核驳回
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(-1);
        shopMapper.update(shop);
        //3.发送修改链接
        Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("123720197@qq.com");
        message.setTo(employee.getEmail());
        message.setSubject("宠物乐园-店铺入驻驳回任务");
        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
                "http://localhost:8081/#/register2?shopId="+log.getShopId());
        javaMailSender.send(message);
    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }

    @Override
    public void activation(Long shopId) {
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(3);
        shopMapper.update(shop);
    }


}
